﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Enter.DataProxy.DbConnector;

namespace Enter.DataProxy.Mail
{
    public class PageEntity
    {
      
        public enum EnumPrimaryKeyType
        {
            Num,
            String,
            DateTime,
        }
        private string _tbName = "";
        /// <summary>
        /// 表名(可多表)a mm inner join b m ON a.memId=b.ID
        /// </summary>
        public string tbName
        {
            get { return _tbName; }
            set { _tbName = value; }
        }
        private string _PKey = "ID";
        /// <summary>
        /// 主键
        /// </summary>
        public string PKey
        {
            get { return _PKey; }
            set { _PKey = value; }
        }
        private EnumPrimaryKeyType _PKeyType = EnumPrimaryKeyType.Num;
        /// <summary>
        /// 主键的类型,0-数字类型,1-字符类型,2-日期时间类型
        /// </summary>
        public EnumPrimaryKeyType PKeyType
        {
            get { return _PKeyType; }
            set { _PKeyType = value; }
        }
        private string _Orderby = "ID desc";
        /// <summary>
        /// 排序字符串
        /// </summary>
        public string Orderby
        {
            get { return _Orderby; }
            set { _Orderby = value; }
        }
        private string _Collist = "*";
        /// <summary>
        /// 要查询出的字段列表,*表示全部字段
        /// </summary>
        public string Collist
        {
            get { return _Collist; }
            set { _Collist = value; }
        }
        /// <summary>
        /// 查询条件
        /// </summary>
        private string _Where = "";
        public string Where
        {
            get { return _Where; }
            set { _Where = value; }
        }
        private int _PageSize;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }
        private int _CurrentPageIndex;
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPageIndex
        {
            get { return _CurrentPageIndex; }
            set { _CurrentPageIndex = value; }
        }
        private int _TotalPages;
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get { return _TotalPages; }
            set { _TotalPages = value; }
        }
        private int _TotalCount;
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCount
        {
            get { return _TotalCount; }
            set { _TotalCount = value; }
        }
        private int _CurCount;
        /// <summary>
        /// 当前条数
        /// </summary>
        public int CurCount
        {
            get { return _CurCount; }
            set { _CurCount = value; }
        }
      
    }
   public class BaseManager : DataProxyManager
    {
       public BaseManager()
            : base(ProxyProvider.ConfigurationName, ProxyProvider.ConfiruationDbName)
        { 
               
        }
       
       /// <summary>
       /// 利用分页存储过程
       /// </summary>
       public DataTable GetTable(PageEntity entity)
       {
          
           try
           {

               var db = base.CreateDbReader();
               ParameterCollection parameters = CreateParameter();
               parameters.Append("tbName", entity.tbName, DbType.String);
               parameters.Append("colName", entity.PKey, DbType.String);
               parameters.Append("coltype", (int)entity.PKeyType, DbType.Int32);
               parameters.Append("orderby", entity.Orderby, DbType.String);
               parameters.Append("collist", entity.Collist, DbType.String);
               parameters.Append("pagesize", entity.PageSize, DbType.Int32);
               parameters.Append("page", entity.CurrentPageIndex, DbType.Int32);
               parameters.Append("condition", entity.Where, DbType.String);
               parameters.Append("pages", 0,DbType.Int32);
               parameters.Append("rsCount", 0,DbType.Int32);
               parameters.Append("curCount", 0,DbType.Int32);

               parameters["@pages"].Direction = ParameterDirection.Output;
               parameters["@rsCount"].Direction = ParameterDirection.Output;
               parameters["@curCount"].Direction = ParameterDirection.Output;
               DataTable dt=  db.ExecProcdureDataSet("sp_GetPage",parameters).Tables[0];
               entity.TotalPages = (int)parameters["@pages"].Value;
               entity.TotalCount = (int)parameters["@rsCount"].Value;
               entity.CurCount = (int)parameters["@curCount"].Value;
               return dt;
               
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
         
       }

       /// <summary>
       /// 利用分页存储过程
       /// </summary>
       public IList<T> GetList<T>(PageEntity entity)
       {

           try
           {

               var db = base.CreateDbReader();
               ParameterCollection parameters = CreateParameter();
               parameters.Append("tbName", entity.tbName, DbType.String);
               parameters.Append("colName", entity.PKey, DbType.String);
               parameters.Append("coltype", (int)entity.PKeyType, DbType.Int32);
               parameters.Append("orderby", entity.Orderby, DbType.String);
               parameters.Append("collist", entity.Collist, DbType.String);
               parameters.Append("pagesize", entity.PageSize, DbType.Int32);
               parameters.Append("page", entity.CurrentPageIndex, DbType.Int32);
               parameters.Append("condition", entity.Where, DbType.String);
               parameters.Append("pages", 0, DbType.Int32);
               parameters.Append("rsCount", 0, DbType.Int32);
               parameters.Append("curCount", 0, DbType.Int32);

               parameters["@pages"].Direction = ParameterDirection.Output;
               parameters["@rsCount"].Direction = ParameterDirection.Output;
               parameters["@curCount"].Direction = ParameterDirection.Output;
               IList<T> list = db.ExecProcdure<T>("sp_GetPage", parameters);
               entity.TotalPages = (int)parameters["@pages"].Value;
               entity.TotalCount = (int)parameters["@rsCount"].Value;
               entity.CurCount = (int)parameters["@curCount"].Value;
               return list;

           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }

       }
    }
}
